﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Simhopp.Common
{
    /// <summary>
    /// Class Dive, describes a dive with a difficulty factor and scores.
    /// </summary>
    public class Dive
    {
        private double difficulty_factor;
        private List<Score> scores;
        private int number_of_scores;

        public Dive(int number_of_scores = 7, double difficulty_factor = 0.0)
        {
            this.number_of_scores = number_of_scores;
            this.difficulty_factor = difficulty_factor;
            this.scores = new List<Score>();
        }

        public List<Score> Scores
        {
            get { return scores; }
        }
        public double DifficultyFactor
        {
            get { return difficulty_factor; }
            set { difficulty_factor = value; }
        }

        public void add_score(Score score)
        {
            if (scores.Count == number_of_scores)
                throw new Exception("Cannot add more scores.");
            else
                this.scores.Add(score);
        }

        public double total_score()
        {
            if (scores.Count == 0)
                throw new Exception("There are no scores associated with this dive.");
            else
            {
                double sum = 0.0;
                double max = scores[0].Value;
                double min = scores[0].Value;

                foreach (var score in scores)
                {
                    if (score.Value > max)
                        max = score.Value;
                    else if (score.Value < min)
                        min = score.Value;
                    sum += score.Value;
                }
                sum = (sum - max - min) * difficulty_factor; 
                return sum;
            }
        }
    }
}
